package controllers

import (
	"fmt"
	"portstoragechanges/models"
	"portstoragechanges/pkg/app"
	"portstoragechanges/pkg/e"
	"portstoragechanges/services"

	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func Getenter(c *gin.Context) {
	err, data := services.Getenter()
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
	}
	app.OK(c, gin.H{"value": data}, "查询成功")
}

func Delenter(c *gin.Context) {
	data := "-1"
	data = c.Query("enterid")
	c.ShouldBindJSON(&data)
	if data == "-1" {
		app.INFO(c, 30001, "参数错误")
		return
	}
	err := services.Delenter(data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": data}, "删除成功")
}

func Updateenter(c *gin.Context) {
	var data models.Enterreceive
	c.ShouldBindJSON(&data)
	var datanew models.Enter
	datanew.Enter_id = data.Enter_id
	datanew.Enter_date = data.Enter_date
	datanew.car_number = com.StrTo(data.car_number).MustInt()
	datanew.Enter_ton = data.Enter_ton
	datanew.Enter_piece = com.StrTo(data.Enter_piece).MustInt()
	err := services.Updateenter(datanew)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": datanew}, "更新成功！")
}

func Addenter(c *gin.Context) {
	var data models.Enterreceive
	err := c.ShouldBindJSON(&data)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	fmt.Println(data)
	var datanew models.Enter
	datanew.Enter_id = data.Enter_id
	datanew.Enter_date = data.Enter_date
	datanew.car_number = com.StrTo(data.car_number).MustInt()
	datanew.Enter_ton = data.Enter_ton
	datanew.Enter_piece = com.StrTo(data.Enter_piece).MustInt()
	err = services.Addenter(datanew)
	if err != nil {
		app.Error(c, e.ERROR, err, err.Error())
		return
	}
	app.OK(c, gin.H{"value": datanew}, "添加成功！")
}
